package com.zhong.gang.app.utils;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhong.gang.applib.R;
import com.zhong.gang.applib.bean.MaterialBean;
import com.zhong.gang.applib.utils.NumberUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Locale;


public class ShouFaView extends LinearLayout implements ShouFaItemView.ItemListener {
    private boolean isShow;
    private TextView textView;
    private double allWeight;
    private MonenyChangedListener listener;
    private int type, SteelMillId;
    private double money;

    public int getSteelMillId() {
        return SteelMillId;
    }

    public double getMoney() {
        return money;
    }

    public void setData(int SteelMillId, int type, double money) {
        boolean staus = false;
        if (SteelMillId != this.SteelMillId || type != this.type) {
            removeAllViews();
            staus = true;
        }
        this.SteelMillId = SteelMillId;
        this.type = type;
        this.money = money;
        if (staus && SteelMillId > 0)
            addView();
    }

    public void setListener(MonenyChangedListener listener) {
        this.listener = listener;
    }

    public void setText(TextView text) {
        if (text == null)
            return;
        textView = text;
        text.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                removeView();
            }
        });
    }

    public void setShow(boolean show) {
        isShow = show;
    }

    public ShouFaView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.setOrientation(VERTICAL);
        TypedArray array = getContext().obtainStyledAttributes(attrs, R.styleable.ShouFaView);
        isShow = array.getBoolean(R.styleable.ShouFaView_show, true);
    }

    /**
     * 放置所有重量
     */
    public void setAllWeight(double allWeight) {
        this.allWeight = allWeight;
        showView();
    }


    /**
     * 没有数据的时候添加这个时候需要用户去手动添加
     */
    public void addView() {
        ShouFaItemView faHuoItemView =
                new ShouFaItemView(getContext(), isShow, type, SteelMillId, money);
        faHuoItemView.setListener(this);
        this.addView(faHuoItemView);
        showView();
    }

    /**
     * 这个时候是详情
     */
    public void addView(MaterialBean bean) {
        ShouFaItemView faHuoItemView = new ShouFaItemView(getContext(), isShow, bean);
//        faHuoItemView.setListener(this);
        this.addView(faHuoItemView);
    }

    @Override
    public void showView() {
        if (textView != null) {
            if (getChildCount() >= 2)
                textView.setVisibility(VISIBLE);
            else
                textView.setVisibility(GONE);
        }
        double weight = 0, allMoneny = 0;
        for (int n = 0; n < getChildCount(); n++) {
            View view = getChildAt(n);
            if (view instanceof ShouFaItemView) {
                ShouFaItemView itemView = (ShouFaItemView) view;
                double itemWeight = setItemView(itemView, n >= getChildCount() - 1);
                if (n < getChildCount() - 1) {
                    itemView.getBinding().weight.addTextChangedListener(itemView);
                    weight = weight + itemWeight;
                } else {
                    double nextWeight = allWeight - weight;
                    ((ShouFaItemView) view).getBinding().weight.setText(
                            nextWeight > 0 ? String.format(
                                    Locale.CHINA, "%.2f", (float) nextWeight) : "0");
                }
                allMoneny = getMoney(itemView) + allMoneny;
            }
        }
        if (listener != null)
            listener.afterMonenyChanged(allMoneny);
    }

    /**
     * 获取Item的金额
     *
     * @param itemView
     * @return
     */
    private double getMoney(ShouFaItemView itemView) {
        double money = 0.0;
        try {
            double moneny = Double.parseDouble(itemView.getBinding().moneny.getText().toString());
            double num = Double.parseDouble(itemView.getBinding().weight.getText().toString());
            money = moneny * num;
        } catch (Exception e) {
        }
        return NumberUtils.getNumber(money);
    }

    /**
     * 设置Item
     *
     * @param itemView
     * @param status
     */
    private double setItemView(ShouFaItemView itemView, boolean status) {
        itemView.getSelect().setEnabled(status);
        itemView.getSelect().setTag(status);
        itemView.getSelect()
                .setImageResource(status ? R.mipmap.icon_gou : R.drawable.bg_transparent);
        itemView.getBinding().weight.setEnabled(!status);
        itemView.getBinding().weight.removeTextChangedListener(itemView);
        return NumberUtils.getNumber(itemView.getBinding().weight.getText().toString());
    }

    @Override
    public void viewStatus(boolean isAdd) {
        if (isAdd) {
            addView();
        } else
            removeView();
    }

    public void removeView() {
        if (getChildCount() > 1) {
            ShouFaView.this.removeViewAt(getChildCount() - 1);
        }
        showView();
    }


    public String getJsonArray() {
        JSONArray array = new JSONArray();
        for (int n = 0; n < getChildCount(); n++) {
            View view = getChildAt(n);
            if (view instanceof ShouFaItemView) {
                try {
                    ShouFaItemView faItemView = (ShouFaItemView) view;
                    if (faItemView.getBean() == null) {
                        return null;
                    }
                    JSONObject object = new JSONObject();
                    object.put("materialTypeId", faItemView.getBean().getId());
                    object.put("price", NumberUtils.getNumber(
                            faItemView.getBinding().moneny.getText().toString()) + "");
                    object.put("weight", NumberUtils.getNumber(
                            faItemView.getBinding().weight.getText().toString()) + "");
                    array.put(object);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
        return array.toString();
    }


    public interface MonenyChangedListener {
        void afterMonenyChanged(double allMoneny);
    }
}
